#! /usr/bin/python 
# -*- coding: utf-8 -*-

import sys
import os
import time
import getopt


def Operate(fMetric,fBug):
	fMetricDict = {}
	fBugDict = {}

	f1=open(fMetric, 'r')
	lines1= f1.readlines()
	f1.close()
	for line1 in lines1:
		if len(line1) > 2:
			#print line1
			fMetricDict[line1.split(',')[0]]=line1;
	
	f2=open(fBug, 'r')
	lines2= f2.readlines()
	f2.close()

	for line2 in lines2:
		if len(line2) > 2:
			tracfilename=line2.split(',')[0]
			fBugDict[tracfilename.split('\n')[0]]=1
	
	reportFileName = "trainFile.csv"
	report= open(reportFileName,'w')
	for filename,metrics in fMetricDict.iteritems():
		#print filename
		filename=filename.strip("\n")
		if filename.strip("\n") in fBugDict:
			metrics=metrics.strip()
			report.write("%s,%s\n" % (metrics,"true"))			
			#report.write(filename)
			#report.write("\n")
		else:	
			metrics=metrics.strip()
			report.write("%s,%s\n" % (metrics,"false"))

def ShowHelp():
    print "Usage: fileMatch.py --fMetric [metric file path] --fBug [bug file path]"    
    sys.exit(0)

if __name__ == "__main__":
	fileMetric = None
	fileBug = None
	try:
        	opts, args = getopt.getopt(sys.argv[1:],"f1:f2",["fMetric=","fBug="])
        	for opt, arg in opts:
			if opt in ("-f1", "--fMetric"):
				fileMetric = arg
			elif opt in ("-f2", "--fBug"):
				fileBug = arg
		if len(sys.argv) == 1:
            		ShowHelp()
		elif not fileMetric:
			print "Metric File was not specified"
			ShowHelp()
		elif not fileBug:
			print "Bug File was not specified"
			ShowHelp()
		else:
			Operate(fileMetric,fileBug)
	except getopt.GetoptError:
        	ShowHelp()

